package com.jx.gocomtask.webservice.service.impl;

import com.jx.gocomtask.webservice.entity.ExtTaskSearchHistory;
import com.jx.gocomtask.webservice.repository.ExtTaskSearchHistoryRepository;
import com.jx.gocomtask.webservice.service.ExtTaskSearchHistoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.sql.Timestamp;
import java.util.List;

/**
 * Created by LWD on 2017/8/21.
 */
@Service
public class ExtTaskSearchHistoryServiceImpl implements ExtTaskSearchHistoryService {
    @Resource
    ExtTaskSearchHistoryRepository extTaskSearchHistoryRepository;
    @Override
    public List<ExtTaskSearchHistory> findList(String userId) {
        List<ExtTaskSearchHistory> list = extTaskSearchHistoryRepository.findBySearchUserOrderByCreateTimeDesc(userId);
        return list;
    }

    @Override
    @Transactional
    public void add(String userId, String content) {
        List<ExtTaskSearchHistory> list = extTaskSearchHistoryRepository.findBySearchUserOrderByCreateTimeDesc(userId);

        for(ExtTaskSearchHistory history1 : list){
            if(history1.getContent().equals(content))
                return;
        }
        Long time = System.currentTimeMillis();
        ExtTaskSearchHistory history = null;
        if(null == list || list.size() == 0 || list.size() < 6){
            history = new ExtTaskSearchHistory();
            history.setSearchUser(userId);
            history.setContent(content);
        }else{
            history = list.get(5);
            history.setContent(content);
        }
        history.setCreateTime(new Timestamp(time));

        extTaskSearchHistoryRepository.save(history);
    }
}
